package jsyf.oa.dao.email;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.email.EmailMainEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by LT on 2018/3/7.
 */
@Repository
public class SentEmailDao extends BaseDao {
    public SentEmailDao() {
        super.iBaseEntity = new EmailMainEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, EmailMainEntity.class);
    }

    public List<JSONObject> getByEmailReceiver(String emailId, String userType) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" receiver.USER_ID AS userId, \n");
        sql.append(" receiver.USER_NAME AS userName\n");
        sql.append(" FROM \n");
        sql.append(" T_SYS_EMAIL_RECEIVER AS receiver \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND receiver.EMAIL_ID = ? AND receiver.USER_TYPE = ? \n");
        Object[] params = {emailId, userType};
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);
        return listResult;
    }
    public List<JSONObject> getListByPage(String condition,int requestPage,int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT \n" +
                " email.ID AS id, \n" +
                " email. SUBJECT AS subject, \n" +
                " email.CONTENT AS content, \n" +
                " email.CREATE_USER_ID AS createUserId, \n" +
                " email.CREATE_TIME AS createTime, \n" +
                " email.E_TYPE AS etype, \n" +
                " email.DATA_ID AS dataId, \n" +
                " tUser.NAME AS sender \n" +
                " FROM \n" +
                " t_sys_email AS email \n" +
                " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                " WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(" GROUP BY email.ID ");
        sql.append(
                " UNION ALL\n" +
                " SELECT \n" +
                " email.ID AS id, \n" +
                " email. SUBJECT AS subject, \n" +
                " email.CONTENT AS content, \n" +
                " email.CREATE_USER_ID AS createUserId, \n" +
                " email.CREATE_TIME AS createTime, \n" +
                " email.E_TYPE AS etype, \n" +
                " email.DATA_ID AS dataId, \n" +
                " tUser.NAME AS sender \n" +
                " FROM \n" +
                " t_sys_email_all AS email \n" +
                " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                " WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append("  ORDER BY createTime DESC");
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> listResult = super.queryForJsonList(sql.toString());
        return listResult;
    }

    public Integer getListByPageCount(String condition) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select sum(tmpcount) from (  SELECT \n" +
                " count(1) as tmpcount \n" +
                " FROM \n" +
                " t_sys_email AS email \n" +
                " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                " WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(
                "UNION ALL\n" +
                        " SELECT \n" +
                        " count(1) as tmpcount \n" +
                        " FROM \n" +
                        " t_sys_email_all AS email \n" +
                        " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                        " WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(" ) email");
        String count = super.queryForString(sql.toString());
        if (count == null) {
            count = "0";
        }
        return Integer.parseInt(count);
    }
    public List<JSONObject> getByAttaches(String emailId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" attach.ID AS id, \n");
        sql.append(" attach.FILE_NAME AS name, \n");
        sql.append(" attach.FILE_URL AS url \n");
        sql.append(" FROM \n");
        sql.append(" T_ATTACH_MODULE AS attach_module \n");
        sql.append(" LEFT JOIN T_ATTACHMENT AS attach ON attach_module.ATTACH_ID = attach.ID \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND attach_module.MODULE_ID = ? \n");
        Object[] params = {emailId};

        // 查询结果
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);

        return listResult;
    }

}
